home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / transitions.win / src / transitioneffect.java
Encoding:
Java Source  |  2000-06-23  |  5.6 KB  |  209 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.io.*;
  12.  
  13. import quicktime.std.StdQTConstants;
  14. import quicktime.*;
  15. import quicktime.qd.*;
  16. import quicktime.io.*;
  17. import quicktime.std.image.*;
  18. import quicktime.std.movies.*;
  19. import quicktime.util.*;
  20.  
  21. import quicktime.app.QTFactory;
  22. import quicktime.app.time.*;
  23. import quicktime.app.image.*;
  24. import quicktime.app.display.*;
  25. import quicktime.app.anim.*;
  26. import quicktime.app.players.*;
  27. import quicktime.app.spaces.*;
  28. import quicktime.app.actions.*;
  29.  
  30.  
  31. public class TransitionEffect extends Frame implements StdQTConstants, QDConstants {
  32.     
  33.     public static void main(String args[]) {
  34.         try { 
  35.             QTSession.open();
  36.             
  37.             TransitionEffect te = new TransitionEffect("Transition Effect");
  38.             te.pack();
  39.             te.show();
  40.             te.toFront();    
  41.         } catch (Exception e) {
  42.             e.printStackTrace();
  43.             QTSession.close();
  44.         }
  45.     
  46.     }
  47.     
  48.     
  49.  
  50.     TransitionEffect(String title) throws Exception {
  51.         super (title);
  52.  
  53.         QTCanvas myQTCanvas = new QTCanvas(QTCanvas.kInitialSize, 0.5f, 0.5f);
  54.         add("Center", myQTCanvas);
  55.         
  56.         Dimension d = new Dimension (300, 300);
  57.         addWindowListener(new WindowAdapter() {
  58.             public void windowClosing (WindowEvent e) {
  59.                 QTSession.close();
  60.                 dispose();
  61.             }
  62.             
  63.             public void windowClosed (WindowEvent e) { 
  64.                 System.exit(0);
  65.             }
  66.         });
  67.  
  68.         QDGraphics gw = new QDGraphics (new QDRect(d));
  69.         Compositor comp = new Compositor (gw, QDColor.black, 20, 1);
  70.  
  71.         ImagePresenter idb = makeImagePresenter (new QTFile (QTFactory.findAbsolutePath ("pics/stars.jpg")),
  72.                                     new QDRect(300, 220));
  73.         idb.setLocation (0, 0);
  74.         comp.addMember (idb, 2);
  75.  
  76.         ImagePresenter id = makeImagePresenter (new QTFile (QTFactory.findAbsolutePath ("pics/water.pct")),
  77.                                     new QDRect(300, 80));
  78.         id.setLocation (0, 220);
  79.         comp.addMember (id, 4);
  80.         
  81.         CompositableEffect ce = new CompositableEffect ();
  82.         AtomContainer effectSample = new AtomContainer(); 
  83.         effectSample.insertChild (new Atom(kParentAtomIsContainer), 
  84.                         kEffectWhatAtom, 
  85.                         1, 
  86.                         0,
  87.                         EndianOrder.flipNativeToBigEndian32(kWaterRippleCodecType));
  88.         ce.setEffect (effectSample);
  89.         ce.setDisplayBounds (new QDRect(0, 220, 300, 80));
  90.         comp.addMember (ce, 3);
  91.         
  92.         Fader fader = new Fader();        
  93.         QTEffectPresenter efp = fader.makePresenter();
  94.         efp.setGraphicsMode (new GraphicsMode (blend, QDColor.gray));
  95.         efp.setLocation(80, 80);
  96.         comp.addMember (efp, 1);
  97.  
  98.         comp.addController(new TransitionControl (20, 1, fader.getTransition()));
  99.  
  100.         myQTCanvas.setClient (comp, true);
  101.         comp.getTimer().setRate(1);
  102.     }
  103.     
  104.     private ImagePresenter makeImagePresenter (QTFile file, QDRect size) throws Exception {
  105.         GraphicsImporterDrawer if1 = new GraphicsImporterDrawer (file);
  106.         if1.setDisplayBounds (size);
  107.         return ImagePresenter.fromGraphicsImporterDrawer (if1);
  108.     }
  109. }
  110.  
  111. class Fader implements StdQTConstants {
  112.     Fader() throws Exception {
  113.         File file = QTFactory.findAbsolutePath ("pics/Ship.pct");
  114.         QTFile f = new QTFile (file.getAbsolutePath());
  115.  
  116.         QDGraphics g = new QDGraphics (new QDRect (78, 29));
  117.         g.setBackColor (QDColor.black);
  118.         g.eraseRect(null);
  119.         ImagePresenter srcImage = ImagePresenter.fromGWorld(g);
  120.         Compositable destImage = new GraphicsImporterDrawer (f);
  121.         
  122.         ef = new QTTransition ();
  123.         ef.setRedrawing(true);
  124.         ef.setSourceImage (srcImage);
  125.         ef.setDestinationImage (destImage);
  126.         ef.setDisplayBounds (new QDRect(78, 29));
  127.         ef.setEffect (createFadeEffect (kEffectBlendMode, kCrossFadeTransitionType));
  128.         ef.setFrames(60);
  129.         ef.setCurrentFrame(0);
  130.     }
  131.     
  132.     private QTTransition ef;
  133.     
  134.     public QTEffectPresenter makePresenter() throws QTException {
  135.         QTEffectPresenter efPresenter = new QTEffectPresenter (ef);
  136.         return efPresenter;
  137.     }    
  138.     
  139.     public QTTransition getTransition () {
  140.         return ef;
  141.     }
  142.     
  143.     AtomContainer createFadeEffect (int effectType, int effectNumber) throws QTException {
  144.         AtomContainer effectSample = new AtomContainer();
  145.         effectSample.insertChild (new Atom(kParentAtomIsContainer), 
  146.                                 kEffectWhatAtom, 
  147.                                 1,
  148.                                 0, 
  149.                                 EndianOrder.flipNativeToBigEndian32(kCrossFadeTransitionType));
  150.         effectSample.insertChild (new Atom(kParentAtomIsContainer), 
  151.                                 effectType, 
  152.                                 1, 
  153.                                 0, 
  154.                                 EndianOrder.flipNativeToBigEndian32(effectNumber));
  155.         return effectSample;
  156.     }
  157. }    
  158.  
  159. class TransitionControl extends PeriodicAction implements TicklishController {
  160.  
  161.     TransitionControl (int scale, int period, QTTransition t) {
  162.         super (scale , period);
  163.         this.t = t;            
  164.     }
  165.     
  166.     
  167.     QTTransition t;
  168.     boolean waiting = false;
  169.     int startWaitTime = 0;
  170.     int waitForMsecs = 4000;
  171.     
  172.     public void addedToSpace (Space s) {}
  173.  
  174.     public void removedFromSpace () {}
  175.     
  176.     protected boolean constraintReached () {
  177.         return false;
  178.     }
  179.     
  180.     public void timeChanged (int tm) throws QTException {
  181.         if (waiting)
  182.             startWaitTime = tm;
  183.         super.timeChanged(tm);
  184.     }
  185.     
  186.     protected void doAction (float er, int tm) throws QTException {
  187.         if (waiting) {
  188.             if ((er > 0 && ((startWaitTime + waitForMsecs) <= tm))
  189.                 || (er < 0 && ((startWaitTime - waitForMsecs) >= tm))) {
  190.                 
  191.                 waiting = false;
  192.                 t.setRedrawing(true);
  193.             } else
  194.                 return;
  195.         }
  196.         int curr_frm = t.getCurrentFrame();
  197.         curr_frm++;
  198.         t.setCurrentFrame(curr_frm);
  199.         if (curr_frm > t.getFrames()) {
  200.             curr_frm = 0;
  201.             t.setRedrawing(false);
  202.             t.setCurrentFrame(curr_frm);    
  203.             t.swapImages();
  204.             waiting = true;
  205.             startWaitTime = tm;
  206.         }
  207.     }    
  208. }    
  209.